home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bisonpcb / warshall.c < prev   
C/C++ Source or Header  |  1987-09-18  |  3KB  |  126 lines

  1. /* Generate transitive closure of a matrix,
  2.    Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /*
  22.  * Port to PC by Whit Gregg
  23.  *               Nourse, Gregg & Browne, Inc.
  24.  *         1 Horizon Road
  25.  *         Fort Lee, NJ  07024
  26.  */
  27. /****************************************************************************
  28.  * Sep 18, 1987
  29.  * changed cast from (unsigned *)((unsigned)
  30.  *              to   (unsigned *)((unsigned long)
  31.  *
  32.  * to allow the use of the MS compact model -- 32 bit addresses
  33.  *
  34.  * Bill Davis
  35.  *    34 Mary Agnes Road
  36.  *    Framingham, Ma.
  37.  *    01701
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include "machine.h"
  42. #include "func.h"
  43.  
  44. /* given n by n matrix of bits R, modify its contents
  45.    to be the transive closure of what was given.  */
  46.  
  47.     void 
  48. TC(R, n)            /* WG */
  49.     unsigned *R;
  50.     int n;
  51. {
  52.     register unsigned rowsize;
  53.     register unsigned mask;
  54.     register unsigned *rowj;
  55.     register unsigned *rp;
  56.     register unsigned *rend;
  57.     register unsigned *ccol;
  58.     unsigned *relend;
  59.     unsigned *cword;
  60.     unsigned *rowi;
  61.  
  62.     rowsize = WORDSIZE(n) * sizeof(unsigned);
  63.     relend = (unsigned *) ((unsigned long) R + n * rowsize); /* BD */
  64.  
  65.     cword = R;
  66.     mask = 1;
  67.     rowi = R;
  68.     while (rowi < relend) {
  69.         ccol = cword;
  70.         rowj = R;
  71.  
  72.         while (rowj < relend) {
  73.             if (*ccol & mask) {
  74.                 rp = rowi;
  75.                 rend = (unsigned *) ((unsigned long) rowj + rowsize); /* BD */
  76.                 while (rowj < rend)
  77.                     *rowj++ |= *rp++;
  78.                 }
  79.             else {
  80.                 rowj = (unsigned *) ((unsigned long) rowj + rowsize); /* BD */
  81.                 }
  82.  
  83.             ccol = (unsigned *) ((unsigned long) ccol + rowsize); /* BD */
  84.             }
  85.  
  86.         mask <<= 1;
  87.         if (mask == 0) {
  88.             mask = 1;
  89.             cword++;
  90.             }
  91.  
  92.         rowi = (unsigned *) ((unsigned long) rowi + rowsize); /* BD */
  93.         }
  94.     }
  95.  
  96.  
  97. /* Reflexive Transitive Closure.  Same as TC
  98.    and then set all the bits on the diagonal of R.  */
  99.  
  100. void 
  101. RTC(R, n)            /* WG */
  102.     unsigned *R;
  103.     int n;
  104. {
  105.     register unsigned rowsize;
  106.     register unsigned mask;
  107.     register unsigned *rp;
  108.     register unsigned *relend;
  109.  
  110.     TC(R, n);
  111.  
  112.     rowsize = WORDSIZE(n) * sizeof(unsigned);
  113.     relend = (unsigned *) ((unsigned long) R + n * rowsize); /* BD */
  114.     mask = 1;
  115.     rp = R;
  116.     while (rp < relend) {
  117.         *rp |= mask;
  118.         mask <<= 1;
  119.         if (mask == 0) {
  120.             mask = 1;
  121.             rp++;
  122.             }
  123.         rp = (unsigned *) ((unsigned long) rp + rowsize); /* BD */
  124.         }
  125.     }
  126.